package com.yanqu.road.server.cmd.activity.stronghold.wild;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.stronghold.config.StrongholdTimeConfig;
import com.yanqu.road.entity.config.goods.GoodsInfo;
import com.yanqu.road.entity.enums.activity.stronghold.eStrongholdTimeType;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.logic.helper.RandomWeightConfigHelper;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.pb.activity.StrongholdWarCrossProto;
import com.yanqu.road.pb.activity.StrongholdWarProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.manger.activity.strongholdwar.StrongholdActivityMgr;
import com.yanqu.road.server.manger.activity.strongholdwar.StrongholdWarMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.server.manger.system.CommonMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.util.List;

@Cmd(code = GameProtocol.S_STRONGHOLD_WAR_ITEM_DONATE_REQ, desc = "道具捐献请求")
public class StrongholdDonateItemCmd extends AbstractCommand {
    private int type;
    private int itemId;
    private int num;
    private int activityId;
    private String reward;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        StrongholdWarProto.StrongholdItemDonateReqMsg req = StrongholdWarProto.StrongholdItemDonateReqMsg.parseFrom(packet.getBytes());
        type = req.getType();
        itemId = req.getItemId();
        num = Integer.parseInt(req.getNum());
        if (num <= 0) {
            return false;
        }
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        ActivityInfo activityInfo = StrongholdActivityMgr.getActivityInfo();
        if (activityInfo == null) {
            return;
        }
        String unionUid = player.getUserInfo().getUnionUid();

        if (StringUtils.isNullOrEmpty(unionUid)) {
            ret = GameErrorCode.E_STRONGHOLD_NO_UNION;
            return;
        }
        if (!StrongholdActivityMgr.isDonateItemTime()) {
            ret = GameErrorCode.E_STRONGHOLD_CAN_NOT_ITEM_DONATE;
            return;
        }
        activityId = activityInfo.getActivityId();
        // 判断道具是否正确
        int commonItemId = StrongholdActivityMgr.getCommonItemId();
        if (commonItemId == itemId && !StrongholdActivityMgr.isWildTime()) {
            List<StrongholdTimeConfig> configList = StrongholdActivityMgr.getStrongholdTimeConfig();
            for (StrongholdTimeConfig config : configList) {
                if (config.getType() == eStrongholdTimeType.REST.getValue()) {
                    long now = System.currentTimeMillis() / 1000;
                    if (activityInfo.getBeginTime() + config.getBeginTime() * 3600 + 300 > now) {
                        ret = GameErrorCode.E_STRONGHOLD_CALC_RANK;
                        return;
                    }
                }
            }

            if (!StrongholdWarMgr.hasQualification(activityId, unionUid)) {
                //没有资格 且是皇家贡品捐献，不允许捐赠
                ret = GameErrorCode.E_STRONGHOLD_NO_QUALIFICATION_DONATE_COMMON_ITEM;
                return;
            }
        }
        if (itemId != commonItemId) {
            itemId = StrongholdActivityMgr.getDonateItemIdByType(type);
        }
        if (!player.getModule(CurrencyModule.class).removeCurrency(new Property(itemId, BigInteger.valueOf(num)), eLogMoneyType.Stronghold, eLogMoneyType.StrongholdDonateItem)) {
            ret = GameErrorCode.E_BAG_ITEM_NO_ENOUGH;
            return;
        }
        GoodsInfo goodsInfo = CommonMgr.getGoodsInfo(itemId);
        int singleValue = goodsInfo.getParamList().get(0).intValue();
        int addValue = singleValue * num;
        Property reward = new Property();
        for (int i = 0; i < num; i++) {
            String extendParam = goodsInfo.getExtendParam();
            Property property = RandomWeightConfigHelper.getRandomProperty(extendParam);
            reward.addProperty(property);
        }
        if (!reward.isNothing()) {
            this.reward = PropertyHelper.parsePropertyToString(reward);
            player.getModule(CurrencyModule.class).addCurrency(reward, eLogMoneyType.Stronghold, eLogMoneyType.StrongholdDonateItem);
        }
        StrongholdWarCrossProto.StrongholdCrossDonateItemReqMsg.Builder reqMsg = StrongholdWarCrossProto.StrongholdCrossDonateItemReqMsg.newBuilder();
        reqMsg.setActivityId(activityId);
        reqMsg.setUserId(player.getUserId());
        reqMsg.setUnionId(unionUid);
        reqMsg.setType(type);
        reqMsg.setAddValue(addValue);
        reqMsg.setUserInfo(PlayerBasePb.parsePlayerBaseTempMsg(UserMgr.getUserBaseInfo(player.getUserId(), GameServer.getInstance().getServerId())));
        player.sendUnionActivityGroupPacket(CrossProtocol.C_CROSS_STRONGHOLD_DONATE_ITEM, reqMsg, reqMsg.getActivityId());
        StrongholdWarMgr.donateItem(activityId, unionUid, type, addValue);
        StrongholdWarMgr.userDonate(activityId, unionUid, player.getUserId(), type, addValue);
    }

    @Override
    public void sendData(GamePlayer player) {
        StrongholdWarProto.StrongholdItemDonateRespMsg.Builder resp = StrongholdWarProto.StrongholdItemDonateRespMsg.newBuilder();
        resp.setRet(ret);
        if (ret == 0) {
            StrongholdWarProto.StrongholdItemDonateDataMsg.Builder dataMsg = StrongholdWarProto.StrongholdItemDonateDataMsg.newBuilder();
            String unionUid = player.getUserInfo().getUnionUid();
            List<Integer> donateData = StrongholdWarMgr.getDonateItemProcess(activityId, unionUid);
            for (int i = 0; i < donateData.size(); i++) {
                StrongholdWarProto.StrongholdItemDonateTempMsg.Builder tempMsg = StrongholdWarProto.StrongholdItemDonateTempMsg.newBuilder();
                tempMsg.setType(i);
                tempMsg.setValue(donateData.get(i));
                dataMsg.addDonateMsg(tempMsg);
            }
            resp.setDonateMsg(dataMsg);
            if (reward != null) {
                resp.setReward(reward);
            }
        }
        resp.setItemId(itemId);
        resp.setNum(num);
        player.sendPacket(ClientProtocol.U_STRONGHOLD_WAR_ITEM_DONATE_REQ, resp);
    }
}
